package com.hhq.sso.common.util;

import cn.hutool.core.util.StrUtil;
import com.hhq.sso.common.constant.TokenConstant;
import com.hhq.sso.common.domain.UserInfo;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;

/**
 * <p>
 * Description: web上下文
 * </p>
 *
 * @author : xiaodong.yang
 * @date : 2024/6/19 15:11
 */
public class WebContext {
    private static final String BLADE_USER_REQUEST_ATTR = "_BLADE_USER_REQUEST_ATTR_";

    private final static String HEADER = TokenConstant.HEADER;
    private final static String BEARER = TokenConstant.BEARER;
    private final static String CRYPTO = TokenConstant.CRYPTO;
    private final static String ACCOUNT = TokenConstant.ACCOUNT;
    private final static String USER_ID = TokenConstant.USER_ID;
    private final static String ROLE_ID = TokenConstant.ROLE_ID;
    private final static String DEPT_ID = TokenConstant.DEPT_ID;
    private final static String USER_NAME = TokenConstant.USER_NAME;
    private final static String ROLE_NAME = TokenConstant.ROLE_NAME;
    private final static String TENANT_ID = TokenConstant.TENANT_ID;
    private final static String CLIENT_ID = TokenConstant.CLIENT_ID;
    private final static Integer AUTH_LENGTH = TokenConstant.AUTH_LENGTH;
    private static String BASE64_SECURITY;

    /**
     * 获取用户信息
     *
     * @return BladeUser
     */
    public static UserInfo getUser() {
        HttpServletRequest request = WebUtil.getRequest();
        if (request == null) {
            return null;
        }
        // 优先从 request 中获取
        Object userInfo = request.getAttribute(BLADE_USER_REQUEST_ATTR);
        if (userInfo == null) {
            userInfo = getUser(request);
            if (userInfo != null) {
                // 设置到 request 中
                request.setAttribute(BLADE_USER_REQUEST_ATTR, userInfo);
            }
        }
        return (UserInfo)userInfo;
    }

    /**
     * 获取用户信息
     *
     * @param request request
     * @return BladeUser
     */
    public static UserInfo getUser(HttpServletRequest request) {
        return getUser(getClaims(request));
    }

    public static Claims getClaims(HttpServletRequest request) {
        String auth = request.getHeader(HEADER);
        if (StrUtil.isBlank(auth)) {
            auth = request.getParameter(HEADER);
        }
        return null;
    }

    /**
     * 获取用户信息
     *
     * @param claims Claims
     * @return BladeUser
     */
    public static UserInfo getUser(Claims claims) {
        if (claims == null) {
            return null;
        }
        UserInfo userInfo = new UserInfo();
        String clientId = toStr(claims.get(CLIENT_ID));
        Object userIdObject = claims.get(USER_ID);
        if (null != userIdObject) {
            userInfo.setUserId(Long.parseLong(userIdObject.toString()));
        }
        userInfo.setClientId(clientId);
        userInfo.setTenantId(toStr(claims.get(TENANT_ID)));
        userInfo.setAccount(toStr(claims.get(ACCOUNT)));
        userInfo.setRoleId(toStr(claims.get(ROLE_ID)));
        userInfo.setDeptId(toStr(claims.get(DEPT_ID)));
        userInfo.setRoleName(toStr(claims.get(ROLE_NAME)));
        userInfo.setUserName(toStr(claims.get(USER_NAME)));

        return userInfo;
    }

    public static String getTenantId() {
        UserInfo user = getUser();
        if (null == user) {
            return null;
        }
        return user.getTenantId();
    }
    public static String getRoleId() {
        UserInfo user = getUser();
        if (null == user) {
            return null;
        }
        return user.getRoleId();
    }

    public static String toStr(Object str) {
        if (null == str) {
            return "";
        }
        return String.valueOf(str);
    }

}
